﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class SubsidyPaymentDepSummaryController : Controller
    {
        private SubsidyPaymentDepSummaryInfo fundinfo = new SubsidyPaymentDepSummaryInfo();
        public ActionResult SubsidyPaymentDepSummaryIndex()
        {
            return View();
        }

        public ActionResult GetList(PageVM pvm)
        {
            string condition = pvm.query;
            string starttime = condition.Split(',')[0].ToString();
            string endtime = condition.Split(',')[1].ToString();
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = fundinfo.GetListCounttWhere(" and RDATETIME>='" + starttime + "' and RDATETIME<='" + endtime + "'");

            DataSet list = fundinfo.BindList(pvm.rp, pvm.page, " and RDATETIME>='" + starttime + "' and RDATETIME<='" + endtime + "'");

            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                #region
                v = new List<string>();
                v.Add(item["rownumber"].ToString());
                v.Add(item["deptName"].ToString());
                v.Add(item["BTCount"].ToString());
                v.Add(item["BTMoney"].ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item[0].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 食堂零星物品领用表
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string StartTime, string EndTime)
        {
            return EContent(StartTime, EndTime);
        }

        public ExcelContentResult EContent(string startTime, string endTime)
        {
            string starttime = startTime.ToString();
            string endtime = endTime.ToString();

            StringBuilder sBuilder = new StringBuilder();
            DataSet list = fundinfo.GetListAll(" and RDATETIME>='" + starttime + "' and RDATETIME<='" + endtime + "'");
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='3' style='font-size:17px; text-align:center;'>上海港集装箱公司外高桥码头公司食堂餐饮管理信息系统</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='3' style='font-size:25px; text-align:center;'>补贴发放分部门汇总表</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='3' style='font-size:14px; text-align:left;'>统计日期：从" + Convert.ToDateTime(starttime).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;到" + Convert.ToDateTime(endtime).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;部门：全部</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td>部门</td>");
            sBuilder.Append("<td>补贴人次</td>");
            sBuilder.Append("<td>补贴总金额</td>");
            sBuilder.Append("</tr>");
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px;white-space: nowrap;'>");
                sBuilder.Append("<td>" + item["deptName"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["BTCount"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["BTMoney"]), 2, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("</table>");
            DataSet ds1 = fundinfo.GetSum(" and RDATETIME>='" + starttime + "' and RDATETIME<='" + endtime + "'");
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='0'>");
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;合计：" + ds1.Tables[0].Rows[0]["AllCount"].ToString()
                + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + Math.Round(Convert.ToDecimal(ds1.Tables[0].Rows[0]["AllMoney"]), 2, MidpointRounding.AwayFromZero) + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td>&nbsp;&nbsp;打印日期：" + DateTime.Now.ToString("yyyy/MM/dd") + "&nbsp;&nbsp;&nbsp;" +
                "打印时间：" + DateTime.Now.ToString("HH:mm:ss") + "&nbsp;&nbsp;&nbsp;制表人：" +
                ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "zenjianmingxi";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
